package logic

import (
	"context"
	"encoding/json"
	"manuscript-service/models/cache"
	"manuscript-service/models/mysql"
	"time"

	"manuscript-service/manrpc/internal/svc"
	"manuscript-service/manrpc/manrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *manrpc.ListRequest) (*manrpc.ListResponse, error) {
	// todo: add your logic here and delete this line
	var list []*manrpc.ListsMan
	result, _ := cache.Rdb.Get(l.ctx, "an_").Result()
	if result == "" {
		list = mysql.ManList(mysql.DB, list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(l.ctx, result, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &manrpc.ListResponse{
		List: list,
	}, nil
}
